<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>机构级别</title>
<link
	href="${pageContext.request.contextPath}/static/css/bootstrap.min.css"
	type="text/css" rel="stylesheet" />
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/ztree/zTreeStyle.css">

</head>
<body>

	<ul id="treeDemo" class="ztree"></ul>

	<script
		src="${pageContext.request.contextPath}/static/js/jquery-1.12.3.min.js"
		type="text/javascript"></script>
	<script
		src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"
		type="text/javascript"></script>
	<script
		src="${pageContext.request.contextPath}/static/ztree/jquery.ztree.all-3.5.min.js"></script>
	<script type="text/javascript">
		$(function() {
			var setting = {
				check : {
					enable : false
				},
				view : {
					addHoverDom : addHoverDom,
					removeHoverDom : removeHoverDom,
					selectedMulti : false,
					addDiyDom : function(treeId, treeNode) {
						//获取每个节点的小图标
						var icoObj = $("#" + treeNode.tId + "_ico"); // tId = permissionTree_1, $("#permissionTree_1_ico")
						//如果存在替换小图标，就替换小图标

						if (treeNode.icon) {
							icoObj.removeClass("button ico_docu ico_open")
									.addClass("fa fa-fw " + treeNode.icon).css(
											"background", "");
						}
					}
				},
				callback : {
		            // 单击事件
		            onClick : zTreeOnClick,

		        }
			};
			
			 // 初始化根节点
		    function initTree() {
		        $.get(basePath + "/design/detain/initNode?type=0", function(data) {
		            // 设置父节点不显示checkbox
		            data.returnData.node.nocheck = true;
		            zTreeObj = $.fn.zTree.init($("#zTree"), setting,data.returnData.node);
		        });
		    }
		    // 单击事件，向后台发起请求
		    function zTreeOnClick(event, treeId, treeNode) {
		        if (treeNode.level == 0||treeNode.level == 1) {
		            return;
		        }
		        //查询该部门下员工信息
		        alert(treeNode.id);
		       	location.href="${pageContext.request.contextPath}/dept/getStaffsByDid?dept_id=${treeNode.id} ";
		    }
			
			
			function addHoverDom(treeId, treeNode) {
				var aObj = $("#" + treeNode.tId + "_a");
				if ($("#diyBtn_" + treeNode.id).length > 0)
					return;

				if (treeNode.level == 0) {
					var editStr = "<span id='diyBtn_space_" +treeNode.id+ "' ></span>"
							+ "<button type='button' class='diyBtn' id='diyBtn_"
							+ treeNode.id
							+ "' title='"
							+ "增加分部"
							+ "' onfocus='this.blur();'><i class=\"glyphicon glyphicon-plus-sign\" style=\"color:red\"></i></button>";
					aObj.append(editStr);
					var btn = $("#diyBtn_" + treeNode.id);
					if (btn)
						btn.bind("click", function() {;  
						    $('#parent').val(treeNode.id);  
							$('#addModal').modal('show')
						});
				} else if (treeNode.level == 1) {
					var editStr = "<span id='diyBtn_space_" +treeNode.id+ "' ></span>"
							+ "<button type='button' class='diyBtn' id='diyBtn_"
							+ treeNode.id
							+ "' title='"
							+ "增加分部"
							+ "' onfocus='this.blur();'><i class=\"glyphicon glyphicon-plus-sign\" style=\"color:green\"></i></button>";
					aObj.append(editStr);
					var btn = $("#diyBtn_" + treeNode.id);
					if (btn)
						btn.bind("click", function() {
							 $('#parent').val(treeNode.id);  
								$('#addModal').modal('show')
						});
				}
			};
			
			function removeHoverDom(treeId, treeNode) {
				$("#diyBtn_" + treeNode.id).unbind().remove();
				$("#diyBtn_space_" + treeNode.id).unbind().remove();
			};
			

			//var d = [{"id":1,"pid":0,"name":"系统权限菜单","url":null,"icon":"glyphicon glyphicon-th-large","open":true,"checked":false,"children":[{"id":2,"pid":1,"seqno":0,"name":"控制面板","url":"dashboard.htm","icon":"fa fa-desktop","open":true,"checked":false,"children":[]},{"id":6,"pid":1,"seqno":1,"name":"消息管理","url":"message/index.htm","icon":"fa fa-weixin","open":true,"checked":false,"children":[]},{"id":7,"pid":1,"seqno":1,"name":"权限管理","url":"","icon":"fa fa-cogs","open":true,"checked":false,"children":[{"id":8,"pid":7,"seqno":1,"name":"用户管理","url":"user/index.htm","icon":"fa fa-user","open":true,"checked":false,"children":[]},{"id":9,"pid":7,"seqno":1,"name":"角色管理","url":"role/index.htm","icon":"fa fa-graduation-cap","open":true,"checked":false,"children":[]},{"id":10,"pid":7,"seqno":1,"name":"许可管理","url":"permission/index.htm","icon":"fa fa-check-square-o","open":true,"checked":false,"children":[]}]},{"id":11,"pid":1,"seqno":1,"name":"资质管理","url":"","icon":"fa fa-certificate","open":true,"checked":false,"children":[{"id":12,"pid":11,"seqno":1,"name":"分类管理","url":"cert/type.htm","icon":"fa fa-th-list","open":true,"checked":false,"children":[]},{"id":13,"pid":11,"seqno":1,"name":"资质管理","url":"cert/index.htm","icon":"fa fa-certificate","open":true,"checked":false,"children":[]}]},{"id":15,"pid":1,"seqno":1,"name":"流程管理","url":"process/index.htm","icon":"fa fa-random","open":true,"checked":false,"children":[]},{"id":16,"pid":1,"seqno":1,"name":"审核管理","url":"","icon":"fa fa-check-square","open":true,"checked":false,"children":[{"id":17,"pid":16,"seqno":1,"name":"实名认证人工审核","url":"process/cert.htm","icon":"fa fa-check-circle-o","open":true,"checked":false,"children":[]}]}]}];
			//$.fn.zTree.init($("#treeDemo"), setting,d); //异步访问数据
			//本次发送ajax请求的目的获得根树关联的json
			$.getJSON("${pageContext.request.contextPath}/dept/getDeptTree",
					function(data) {
						$.fn.zTree.init($("#treeDemo"), setting, data.tree);
						//得到树对象，参数直接写id，不写#
						/*     var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
						    //获得所有的节点	   
						     for(var i=0;i<data.depts.length;i++){
						    	 //根据树节点的id得到树节点
						   	    var node=treeObj.getNodeByParam("id", data.depts[i].id, null);
						         //将treeObj上的node设置为true
						    	treeObj.checkNode( node, false );
						    } */
					});

		})
	</script>

	<!-- 增加开始 -->
	<div class="modal fade" id="addModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">增加机构</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form" id="add_form">
						<div class="form-group">
							<label for="dept_code" class="col-sm-2 control-label">机构编号</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" placeholder="编号"
									name="dept_code">
							</div>
						</div>
						<div class="form-group">
							<label for="dept_name" class="col-sm-2 control-label">机构名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" placeholder="名称" 
								name="dept_name">
							</div>
						</div>

						<div class="form-group">
							<label for="rank" class="col-sm-2 control-label">机构级别</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" placeholder="级别"
									name="rank">
							</div>
						</div>
						<input type="hidden" id="parent" name="parent_id">
					</form>
				</div>
				<div class="modal-footer">
					
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="add_dept">保存</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	<!-- 增加结束 -->
	
	<script>
	$("#add_dept").click(function() {
		$.ajax({
			url : "addDept",
			type : "post",
			async : true,
			data : $("#add_form").serialize(),
			error : function() {
			},
			success : function(data) {
				alert(data)
				$('#addModal').modal('hide');
				location.href="${pageContext.request.contextPath}/dept/dept-list.jsp";
			}
		});
	});
	</script>

</body>
</html>